﻿/*
 * Copyright 2010 Michael Georgoulopoulos
 * 
 * This file is part of Mordor - Source file comparing program.
 * 
 * Mordor is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Mordor is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Mordor.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Contact: info@convexgames.com
 * Website: http://www.convexgames.com
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Threading;
using System.Text;
using System.Windows.Forms;
using System.IO;

using Microsoft.Win32;

namespace Mordor
{

    public partial class Form1 : Form
    {
        Implementation impl = new Implementation();
        ListViewColumnSorter sorter;
        
        public Form1()
        {
            InitializeComponent();

            sorter = new ListViewColumnSorter();
            listView1.ListViewItemSorter = sorter;

            saveReport.Enabled = false;
            ResetStatus();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Form1_Resize(sender, e);
            
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            listView1.Left = 0;
            listView1.Top = menuStrip1.Height;
            listView1.Width = Width - 15;
            listView1.Height = Height - 85;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (worker.IsBusy)
                worker.CancelAsync();

            ResetStatus();
            Application.Exit();
        }

        private void listView1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.All;
            }
        }

        private void ResetStatus()
        {
            statusStrip1.Items["label"].Text = "Ready, please drag and drop your files.";
            statusStrip1.Items["progress"].Visible = false;
        }

        private void listView1_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                impl.Clear();
                listView1.Items.Clear();

                my_files = (string[])e.Data.GetData(DataFormats.FileDrop);

                saveReport.Enabled = false;

                statusStrip1.Items["label"].Text = "Working...";
                ToolStripProgressBar progress = (ToolStripProgressBar)statusStrip1.Items["progress"];
                progress.Visible = true;

                worker.RunWorkerAsync();
            }
        }

        string[] my_files;

        private void progress_Click(object sender, EventArgs e)
        {

        }

        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            impl.Parse(my_files);

            
        }

        private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (!e.Cancelled)
            {
                if (impl.pairs.Count > 0)
                {
                    foreach (Pair p in impl.pairs)
                    {
                        string[] temp_str = new string[4];
                        temp_str[0] = p.source_a;
                        temp_str[1] = p.source_b;
                        temp_str[2] = p.score.ToString();
                        temp_str[3] = (p.similarity * 100).ToString();

                        ListViewItem item = new ListViewItem(temp_str);

                        listView1.Items.Add(item);
                    }

                    saveReport.Enabled = true;
                }
            }
            ResetStatus();            
        }

        private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            if (e.Column == sorter.SortColumn)
            {
                // Reverse the current sort direction for this column.
                if (sorter.Order == SortOrder.Ascending)
                {
                    sorter.Order = SortOrder.Descending;
                }
                else
                {
                    sorter.Order = SortOrder.Ascending;
                }
            }
            else
            {
                // Set the column number that is to be sorted; default to ascending.
                sorter.SortColumn = e.Column;
                sorter.Order = SortOrder.Ascending;
            }

            // Perform the sort with these new sort options.
            listView1.Sort();

        }

        private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            AboutBox box = new AboutBox();
            box.ShowDialog(this);
        }

        private void saveReportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (sd.ShowDialog() == DialogResult.OK)
            {
                StreamWriter sw = new StreamWriter(sd.FileName);
                string f = "";

                sw.WriteLine("Mordor Report - " + DateTime.Now.ToString());
                sw.WriteLine("");
                sw.WriteLine("");

                foreach (ListViewItem c in listView1.Items)
                {
                    f += c.SubItems[0].Text + ", " + c.SubItems[1].Text + ", Distance: " + c.SubItems[2].Text + ", Similarity: " + c.SubItems[3].Text + "%";
                    
                    sw.WriteLine(f);
                    f = "";
                }
                sw.Close();
            }
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveReport.Enabled = false;
            listView1.Items.Clear();
            impl.Clear();
            ResetStatus();
        }

        private void usingMordorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HelpForm box = new HelpForm();
            box.ShowDialog(this);
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\Convex Games\\Mordor");
            if (key == null)
            {
                key = Registry.CurrentUser.CreateSubKey("Software\\Convex Games\\Mordor");
            }
            if (key.GetValue("hide_help") == null)
            {
                key.Close();
                usingMordorToolStripMenuItem_Click(sender, e);
            }
            key.Close();
        }
    }
}
